package com.wtwd.campus.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.wtwd.campus.entity.SysRole;
import com.wtwd.campus.entity.SysUser;
import com.wtwd.campus.model.vo.PageDataVO;
import com.wtwd.campus.model.vo.ResultVO;
import com.wtwd.campus.model.vo.SysRoleVO;
import com.wtwd.campus.service.SysRoleService;
import com.wtwd.campus.service.SysUserService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zdl
 * @since 2021-06-17
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sys-role")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysUserService sysUserService;

    @ApiOperation(value = "查询角色分页数据", nickname = "zdl", notes = "默认获取直属下级,选择同步下级后,获取所有下级")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "roleName", value = "角色名称", dataType = "String")
            , @ApiImplicitParam(name = "isAllLower", value = "同步下级", dataType = "Boolean", example = "true")
            , @ApiImplicitParam(name = "orgId", value = "组织ID", dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "orgCode", value = "组织编号", dataTypeClass = String.class, example = "当orgId有值时,orgCode必传")
    })
    @GetMapping("/page")
    public ResultVO<PageDataVO<SysRoleVO>> queryPage(@RequestParam(defaultValue = "1") Integer pageNo
            , @RequestParam(defaultValue = "10") Integer pageSize, String roleName, boolean isAllLower, Integer orgId, String orgCode) {
        Page<SysRoleVO> page = new Page<>(pageNo, pageSize);
        SysRole sysRole = new SysRole();
        sysRole.setRoleName(roleName);
        if (isAllLower) {// 查询所有下级
            if (StringUtils.isBlank(orgCode)) {
                SysUser user = sysUserService.getCurrentLoginUser();
                sysRole.setOrgCode(user.getOrgCode());
            } else {
                sysRole.setOrgCode(orgCode);
            }
        } else {// 查询直属下级
            if (orgId != null) {
                sysRole.setOrgId(orgId);
            } else {
                SysUser user = sysUserService.getCurrentLoginUser();
                sysRole.setOrgId(user.getOrgId());
            }
        }
        return ResultVO.ok(sysRoleService.queryPage(page, sysRole));
    }

    @ApiOperation("添加角色信息")
    @DynamicParameters(name = "addRoleJson", properties = {
            @DynamicParameter(name = "orgId", value = "组织ID", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "orgCode", value = "组织编号", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "roleName", value = "角色名称", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "description", value = "角色描述", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "permissions", value = "角色权限", dataTypeClass = String.class, required = true, example = "51,52")
    })
    @PostMapping("/role")
    public ResultVO add(@RequestBody JSONObject param) {
        SysRole sysRole = JSONObject.toJavaObject(param, SysRole.class);
        String nickName = sysUserService.getCurrentLoginUser().getNickName();
        sysRole.setCreateBy(nickName);
        sysRole.setUpdateBy(nickName);
        return sysRoleService.createRole(sysRole, param.getString("permissions"));
    }

    @ApiOperation("更新角色信息")
    @DynamicParameters(name = "updateRoleJson", properties = {
            @DynamicParameter(name = "id", value = "角色id", dataTypeClass = Integer.class, required = true)
            , @DynamicParameter(name = "roleName", value = "角色名称", dataTypeClass = String.class)
            , @DynamicParameter(name = "description", value = "角色描述", dataTypeClass = String.class)
            , @DynamicParameter(name = "permissions", value = "角色权限", dataTypeClass = String.class, example = "51,52")
    })
    @PutMapping("/role")
    public ResultVO update(@RequestBody JSONObject param) {
        SysRole sysRole = JSONObject.toJavaObject(param, SysRole.class);
        String nickName = sysUserService.getCurrentLoginUser().getNickName();
        sysRole.setUpdateBy(nickName);
        return sysRoleService.updateRole(sysRole, param.getString("permissions"));
    }

    @ApiOperation("删除角色信息")
    @ApiImplicitParam(name = "id", value = "角色id", required = true, dataTypeClass = Integer.class, paramType = "path")
    @ApiResponses({
            @ApiResponse(code = 2004, message = "该角色存在用户")
    })
    @DeleteMapping("/role/{id}")
    public ResultVO delete(@PathVariable Integer id) {
        return  sysRoleService.deleteById(id);
    }

    @ApiOperation("获取角色权限")
    @ApiImplicitParam(name = "id", value = "角色id", required = true, dataTypeClass = Integer.class, paramType = "path")
    @GetMapping("/rolePermission/{id}")
    public ResultVO<List<Integer>> rolePermission(@PathVariable Integer id) {
        return ResultVO.ok(sysRoleService.getRolePermissionIdsByRoleId(id));
    }

    @ApiOperation("获取组织角色列表")
    @ApiImplicitParam(name = "orgId", value = "组织id", required = true, dataTypeClass = Integer.class, paramType = "path")
    @GetMapping("/roleList/{orgId}")
    public ResultVO<List<SysRole>> getRoleByOrgId(@PathVariable Integer orgId) {
        List<SysRole> data = sysRoleService.getRoleListByOrgId(orgId);
        return ResultVO.ok(data);
    }
}

